package com.oracle.sbdb.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oracle.sbdb.servlet.model.BugUpdates;
import com.oracle.sbdb.util.JSONUtility;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class Demo
 */
@WebServlet("/demo")
public class Demo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Demo() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String updatesString = request.getParameter("updates");
		System.out.println(updatesString != null);
		if(updatesString != null){
			System.out.println(updatesString != null);
			System.out.println(updatesString.length());
		}
        String acceptjson = "";  
        BufferedReader br = new BufferedReader(new InputStreamReader(  
                (ServletInputStream) request.getInputStream(), "UTF-8"));  
        StringBuffer sb = new StringBuffer("");  
        String temp = null;  
        while ((temp = br.readLine()) != null) {  
            sb.append(temp);  
        }  
        br.close();  
        acceptjson = sb.toString();  
        if (acceptjson != "") {  
        	System.out.println(acceptjson);
        	JSONObject jsonOjbect = JSONObject.fromObject(acceptjson);
        	String bugUpdatesString = jsonOjbect.getString("updates");
        	if(bugUpdatesString != null){
        		BugUpdates bugUpdates = JSONUtility.json2Bean(bugUpdatesString, BugUpdates.class);
        		System.out.println(bugUpdates);
        	}
        }  
	}

}
